package com.eling.elcms.travel.dao.hibernate;

import com.eling.elcms.basedoc.dao.IRoomDao;
import com.eling.elcms.basedoc.model.Room;
import com.eling.elcms.core.dao.hibernate.GenericDaoHibernate;
import com.eling.elcms.travel.dao.ICheckOutSetlDetailDao;
import com.eling.elcms.travel.model.CheckOutSetlDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CheckOutSetlDetailDaoHibernate extends GenericDaoHibernate<CheckOutSetlDetail, Long> implements ICheckOutSetlDetailDao{
	
	@Autowired
	private IRoomDao roomDao;

	public CheckOutSetlDetailDaoHibernate() {
		super(CheckOutSetlDetail.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Room queryByPersonPk(Long pkRoomPersonalInfo) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT    ");
		sb.append("	tc.pkRoom,tc.pkChangeRoom,tc.changeRoomDate   ");
		sb.append("FROM    ");
		sb.append("	travel_changeroom tc    ");
		sb.append("WHERE    ");
		sb.append("	tc.pkPersonalInfo = "+pkRoomPersonalInfo+"    ");
		sb.append("ORDER BY tc.changeRoomDate DESC   ");
		List<Object[]> list = getSession().createNativeQuery(sb.toString()).list();
		if(list.isEmpty()){
			return null;
		}
		Object[] objects = list.get(0);
		String pkRoom = objects[0].toString();
		Long l = Long.parseLong(pkRoom);
		return roomDao.get(l);
	}
}
